import { getBookingChannel, getDomainTypes, getFilterList, getDomainSuffix } from "@/api/data"
import { getFinancialType, getFreezeType, getAddress, getInvoiceType } from "@/api/user"
import Storage from "@/utils/storage.js"

export async function bookingChannel() {
  // let res = Storage.getBookingChannel();
  // if(res == null) {
  //   res = await getBookingChannel();
  //   if(res.code == 200) {
  //     Storage.setBookingChannel(res.data);
  //     return res.data
  //   }
  //   return []
  // } else {
  //   return res
  // }
  let res = await getBookingChannel();
  if(res.code == 200) {
    Storage.setBookingChannel(res.data);
    return res.data
  }
  return []
}

export async function domainTypes() {
  let res = Storage.getDomainTypes();
  if(res == null) {
    res = await getDomainTypes();
    if(res.code == 200) {
      let list = res.data;
      for(let i in list) {
        for(let j in list[i].children) {
          list[i].children[j].children = []
        }
      }
      Storage.setDomainTypes(list);
      return list
    }
    return []
  } else {
    return res
  }
}

export async function domainSuffix() {
  let res = Storage.getDomainSuffix();
  if(res == null) {
    res = await getDomainSuffix();
    if(res.code == 200) {
      Storage.setDomainSuffix(res.data);
      return res.data
    }
    return []
  } else {
    return res
  }
}

export async function financialType() {
  let res = Storage.getFinancialType();
  if(res == null) {
    res = await getFinancialType();
    if(res.code == 200) {
      Storage.setFinancialType(res.data);
      return res.data
    }
    return []
  } else {
    return res
  }
}

// 发票类型
export async function invoiceType() {
  let res = Storage.getInvoiceType();
  if(res == null) {
    res = await getInvoiceType();
    if(res.code == 200) {
      Storage.setInvoiceType(res.data);
      return res.data
    }
    return []
  } else {
    return res
  }
}

export async function freezeType() {
  let res = Storage.getFreezeType();
  if(res == null) {
    res = await getFreezeType();
    if(res.code == 200) {
      Storage.setFreezeType(res.data);
      return res.data
    }
    return []
  } else {
    return res
  }
}

export async function filterList() {
  let res = Storage.getFilterList();
  if(res == null) {
    res = await getFilterList();
    if(res.code == 200) {
      res.data.domainSuffixList = res.data.domainSuffixList.map(item => {
        return {
          name: item
        }
      })
      res.data.domainFeatureList = res.data.domainFeatureList.map(item => {
        return {
          name: item
        }
      })
      res.data.domainLenList = res.data.domainLenList.map(item => {
        return {
          name: item
        }
      })
      Storage.setFilterList(res.data);
      return res.data
    }
    return null
  } else {
    return res
  }
}

export async function addressList() {
  let res = Storage.getAddressList();
  if(res == null) {
    res = await getAddress();
    if(res.code == 200) {
      Storage.setAddressList(res.data.dictVOList);
      Storage.setCountryList(res.data.dictCountryList);
      return res.data.dictVOList
    }
    return null
  } else {
    return res
  }
}

export async function countryList() {
  let res = Storage.getAddressList();
  if(res == null) {
    res = await getAddress();
    if(res.code == 200) {
      Storage.setAddressList(res.data.dictVOList);
      Storage.setCountryList(res.data.dictCountryList);
      return {
        country: res.data.dictCountryList,
        province: res.data.dictVOList
      }
    }
    return null
  } else {
    return {
      country: Storage.getCountryList(),
      province: res
    }
  }
}

const list = [
  {
    value: 1,
    label: '企业组织机构代码证'
  },
  {
    value: 2,
    label: '营业执照'
  },
  {
    value: 3,
    label: '统一社会信用代码证'
  },
  {
    value: 4,
    label: '北京市外国驻华使馆人员子女学校办学许可证'
  },
  {
    value: 5,
    label: '事业单位法人证书'
  },
  {
    value: 6,
    label: '社会团体法人登记证书'
  },
  {
    value: 7,
    label: '民办非企业单位登记证书'
  },
  {
    value: 8,
    label: '律师事务所执业许可证'
  },
  {
    value: 9,
    label: '司法鉴定许可证'
  },
  {
    value: 10,
    label: '宗教活动场所登记证'
  },
  {
    value: 11,
    label: '基金会法人登记证书'
  },
  {
    value: 12,
    label: '部队代号'
  },
  {
    value: 13,
    label: '军队单位对外有偿服务许可证'
  },
  {
    value: 14,
    label: '外国企业常驻代表机构登记证'
  },
  {
    value: 15,
    label: '外国在华文化中心登记证'
  },
  {
    value: 16,
    label: '外国政府旅游部门常驻代表机构批准登记证'
  },
  {
    value: 17,
    label: '境外机构证件'
  },
  {
    value: 18,
    label: '社会服务机构登记证书'
  },
  {
    value: 19,
    label: '民办学校办学许可证'
  },
  {
    value: 20,
    label: '医疗机构执业许可证'
  }
]
// 公司证件类型
export function comCertList() {
  return list
}

// 获取公司证件类型名称
export function getComCerName(value) {
  let res = list.find(item => item.value == value);
  return res ? res.label : '';
}

export function createDateTypeList(s) {
  let date = new Date();
  let dateTypeList = []
  let year = date.getFullYear();
  const month = date.getMonth() + 1;
  for (let i = 0; i < 7; i++) {
    if (i === 0) {
      let m = month - s;
      let str = `今年${m}月之后`
      if (m < 0) {
        year = year - 1
        m = 12 + m - 1;
        str = `${year}年${m}月之后`
      } else if(m === 0) {
        str = `今年`
      }
      let d = '01'
      let startTime = new Date(`${year}-${m < 10 ? '0' + m : m}-${d} 00:00:00`).getTime()
      let endTime = date.getTime()
      dateTypeList.push({
        name: str,
        type: i,
        startTime: startTime,
        endTime: endTime,
      })
    } else if (i === 1) {
      let m = month - s;
      if (m !== 0) {
        let str = `今年${m}月之前`
        if (m < 0) {
          m = 12 + m - 1;
          str = `${year}年${m}月之前`
        }
        let d = '01'
        let startTime = new Date(`${year}-01-01 00:00:00`).getTime()
        let endTime = new Date(`${year}-${m < 10 ? '0' + m : m}-${d} 00:00:00`).getTime() - 24*3600*1000
        dateTypeList.push({
          name:  str,
          type: i,
          startTime: startTime,
          endTime: endTime,
        })
      }
    } else {
      let y = year - i + 1;
      let startTime = new Date(`${y}-01-01 00:00:00`).getTime()
      let endTime = new Date(`${y + 1}-01-01 00:00:00`).getTime()
      dateTypeList.push({
        name: y + "年",
        type: i,
        startTime: startTime,
        endTime: endTime,
      })
    }
  }
  console.log(dateTypeList)
  return dateTypeList;
}
